package com.zjy.software.day16;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;

public class PropertiesDemo {
    public static void main(String[] args) throws Exception
    {
//        testPropertiesStore();
//        testPropertiesLoad();
        testGetSystemProperties();

    }



  public static void testGetSystemProperties() {
    Properties properties = System.getProperties();
    //先获取属性的名字
    Set<String> keys = properties.stringPropertyNames();
            Iterator<String> iterator = keys.iterator();
            while (iterator.hasNext()) {
                String key = (String) iterator.next();
                System.out.println("key:"+key+"   value:"+properties.getProperty(key));
            }

    }



    private static void testPropertiesLoad() throws IOException {
        Properties properties = new Properties();
                properties.load(new FileInputStream("config.properties"));
        String name = properties.getProperty("name");
        System.out.println("name = " + name);
    }

    private static void testPropertiesStore() throws IOException {
        Properties properties = new Properties();
        properties.setProperty("name", "zjy");
        properties.setProperty("age", "20");
        properties.setProperty("address", "北京市朝阳区");
        properties.store(new FileOutputStream("config.properties"), "zjy");
    }
}
